package com.ichoice.rabbitmq.rpc.helper.model;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.Assert;

/**
 * 生产者模型
 *
 * @author xianrensawang
 * @since 2021/1/28
 */
@EqualsAndHashCode(callSuper = false)
@Data
public class ProducerModel extends ProducerModelBackBasic {
    /**
     * 方法名
     */
    private String method;
    /**
     * 类名 spring bean名称
     */
    private String bean;
    /**
     * 方法多参数
     */
    private Object[] args;

    /**
     * 需要将消息发送到的服务对应名称
     */
    private String targetApplication;


    private ProducerModel() {

    }

    public static ProducerModel builder() {
        return new ProducerModel();
    }

    public ProducerModel method(String method) {
        this.method = method;
        return this;
    }

    public ProducerModel bean(String bean) {
        this.bean = bean;
        return this;
    }

    public ProducerModel args(Object... args) {
        this.args = args;
        return this;
    }


    public ProducerModel targetApplication(String targetApplication) {
        this.targetApplication = targetApplication;
        return this;
    }


    public ProducerModel retry(Integer count) {
        this.setRetry(count);
        this.setHasRetry(0);
        return this;
    }


    public ProducerModel retryDelay(String ms) {
        this.setRetryDelay(ms);
        return this;
    }

    public ProducerModel ttl(Integer ttl) {
        this.setTtl(Long.valueOf(ttl));
        return this;
    }

    public ProducerModel ttl(Long ttl) {
        this.setTtl(ttl);
        return this;
    }

    public ProducerModel build() {
        Assert.state(Strings.isNotBlank(targetApplication), "指定服务名称不能为空");
        Assert.state(Strings.isNotBlank(this.bean), "指定服务的bean名称不能为空");
        Assert.state(Strings.isNotBlank(this.method), "指定服务bean对应的方法名称不能为空");
        return this;
    }
}
